import url from '@/js/util/url.js';
import user from '@/js/util/user.js';
import message from '@/js/util/message.js';

let color = {
    colorList: [
        ['#7BC3F5', '#618FF6', '#5659F7', '#6ACE99', '#C0F2B4', '#E0E5DB'],
        ['#55D390', '#3BD4D3', '#3AACFF', '#506CFF', '#895BFF', '#B25BFF'],
        ['#F3E493', '#F39673', '#E46571', '#B64AD0', '#6754BD', '#818DD4'],
        ['#97A0EF', '#A2CA94', '#A3D1E8', '#F8B98C', '#F19A91', '#C39DE1'],
        ['#9573DE', '#DDC6E3', '#DB93C1', '#7080D2', '#70BEE2', '#C4E5F4'],
        ['#71D1DD', '#EC9C81', '#F3E2DC', '#8885D3', '#EBAEA1', '#EBAEA1'],
        ['#ECF1FC', '#A4B7E3', '#6980CD', '#6363A2', '#559B85', '#C8D2B9'],
        ['#FFC0DD', '#DE6B8D', '#7162C9', '#A174CA', '#B8BAFF', '#D8EBF7'],
        ['#7DA2EE', '#6A69F5', '#C565F5', '#FA7FFB', '#F2B5DE', '#ECDDE3'],
        ['#A1E3FD', '#91D0F8', '#81BCF2', '#70A7EB', '#6093E6', '#4F7FDF'],
    ],
    customColorList: [],
    resetCustomColorList: function() {
        let param = {
            token: user.getToken()
        };
        let res = url.getCustomMatchList(param);
        let resuleDatas = res.body.message;
        for (let i = this.customColorList.length - 1; i >= 0; i--) {
            this.customColorList.splice(i, 1);
        }
        for (let i = 0; i < resuleDatas.length; i++) {
            let newAttr = JSON.parse(resuleDatas[i].attribute);
            let newDatas = {
                id: resuleDatas[i].id,
                userid: resuleDatas[i].userid,
                attribute: newAttr
            };
            this.customColorList.splice(i, 0, newDatas);
        }
    },
    getCustomList: function() {
        return this.customColorList;
    },
    deleteCustomColor: function(data) {
        let param = {
            token: user.getToken(),
            id: data.id
        };
        let res = url.deleteCustomMatch(param);
        if (res.state.code === 10000) {
            message.message('删除成功！', 'success');
            this.resetCustomColorList();
        } else {
            message.message(res.body.message, 'error');
        }
    },
    submitEditColor: function(data) {
        let param = {
            token: user.getToken(),
            id: data.id,
            attribute: JSON.stringify(data.attribute)
        };
        let res = url.updateCustomMatch(param);
        if (res.state.code === 10000) {
            message.message('修改成功！', 'success');
            this.resetCustomColorList();
        } else {
            message.message(res.body.message, 'error');
        }
    },
    addCustomColor: function(color) {
        let param = {
            token: user.getToken(),
            attribute: JSON.stringify(color)
        };
        let res = url.insertCustomMatch(param);
        if (res.state.code === 10000) {
            this.editMathindex = null;
            this.customMatchColor = [];
            message.message('添加成功', 'success');
            this.resetCustomColorList();
        } else {
            message.message(res.body.message, 'error');
        }
    }
};

export default color;